package com.gnerv.pabu.basic.core.service.impl;

import com.gnerv.pabu.basic.core.dto.UserInfoDTO;
import com.gnerv.pabu.basic.core.entity.User;
import com.gnerv.pabu.basic.core.entity.UserInfo;
import com.gnerv.pabu.basic.core.mapper.UserInfoMapper;
import com.gnerv.pabu.basic.core.mapper.UserMapper;
import com.gnerv.pabu.basic.core.dto.PersonalDTO;
import com.gnerv.pabu.basic.core.service.IPersonalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Slf4j
@Service
public class PersonalServiceImpl implements IPersonalService {

    @Resource
    UserMapper userMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public boolean updatePersonal(PersonalDTO personalDTO) {
        User user = personalDTO.convert();
        userMapper.updateById(user);

        UserInfoDTO userInfoDTO = personalDTO.getUserInfo();
        if (StringUtils.isEmpty(userInfoDTO)) {
            UserInfo userInfo = userInfoDTO.convert();
            userInfo.setUserId(user.getUkId());
            userInfo.setGmtModified(LocalDateTime.now());
            userInfoMapper.updateById(userInfo);
        }
        return true;
    }

}
